package cn.wildfire.chat.app.extendsion.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;
import com.sourcemuch.im.caiba.R2;

import java.util.List;

import cn.wildfire.chat.app.extendsion.classes.FriendRequestFromSource;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.app.extendsion.widgets.CircleImageView;
import cn.wildfire.chat.kit.contact.newfriend.InviteFriendActivity;
import cn.wildfire.chat.kit.user.UserInfoActivity;
import cn.wildfirechat.model.GroupInfo;
import cn.wildfirechat.model.GroupMember;
import cn.wildfirechat.model.UserInfo;
import cn.wildfirechat.remote.ChatManager;

/**
 * Create by HEKE, 2019/9/25 9:21
 * TODO 群聊增加好友弹窗
 */
public class GroupAddFriendPopWindow implements View.OnClickListener {

    ImageView ivCancelPop;
    CircleImageView ivPopHead;
    TextView tvUserNick;
    Button btnAddFriend;

    private Context mContext;
    private UserInfo userInfo;
    private GroupMember groupMemberSelf;
    private boolean canAddFriend;
    private boolean isFriend;
    private boolean isSelf;
    private String target;
    private GroupInfo groupInfo;

    private View mView;
    private PopupWindow window;

    private boolean showing;

    private void initViews() {
        ivCancelPop = mView.findViewById(R.id.iv_cancel_pop);
        ivPopHead = mView.findViewById(R.id.iv_pop_head);
        tvUserNick = mView.findViewById(R.id.tv_user_nick);
        btnAddFriend = mView.findViewById(R.id.btn_add_friend);
        ivCancelPop.setOnClickListener(this);
        btnAddFriend.setOnClickListener(this);
        ivPopHead.setOnClickListener(this);
        window = new PopupWindow(mContext);
        window.setContentView(mView);
        window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        window.setFocusable(true);
        window.setTouchable(true);
        window.setOutsideTouchable(true);
        window.setBackgroundDrawable(new ColorDrawable(0x30000000));
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                showing = false;
            }
        });
        if (canAddFriend) {
            btnAddFriend.setVisibility(View.VISIBLE);
            if (isSelf || isFriend) {
                btnAddFriend.setVisibility(View.GONE);
            }
        } else {
            btnAddFriend.setVisibility(View.GONE);
        }
        try {
            userInfo = ChatManager.Instance().getUserInfo(userInfo.uid, groupInfo.target, true);
            String text = userInfo.friendAlias;
            if (TextUtils.isTextEmpty(text)) {
                text = userInfo.groupAlias;
                if (TextUtils.isTextEmpty(text)) {
                    text = userInfo.displayName;
                }
            }
            tvUserNick.setText("" + text);
            Glide.with(mContext).load(userInfo.portrait).apply(new RequestOptions().placeholder(R.mipmap.default_header).centerCrop()).into(ivPopHead);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public GroupAddFriendPopWindow(Context context, UserInfo userInfo
            , GroupMember groupMemberSelf, String target, GroupInfo groupInfo, boolean canAddFriend) {
        this.mContext = context;
        this.userInfo = userInfo;
        this.groupMemberSelf = groupMemberSelf;
        this.canAddFriend = canAddFriend;
        this.target = target;
        this.groupInfo = groupInfo;
        this.mView = View.inflate(mContext, R.layout.pop_conversation_head_add, null);
        this.isFriend = ChatManager.Instance().isMyFriend(userInfo.uid) || ChatManager.Instance().isMyBlackList(userInfo.uid);
        this.isSelf = userInfo.uid.equals(ChatManager.Instance().getUserId());
        initViews();
    }

    public boolean isShowing() {
        return showing;
    }

    public void show(View anchorView) {
        if (isShowing()) {
            return;
        }
        if (anchorView == null) {
            window.showAtLocation(mView, Gravity.CENTER, 0, 0);
            showing = true;
        }
        FriendRequestFromSource.setFromsource(FriendRequestFromSource.ADD_FROM_GROUP);
    }

    public void hide() {
        if (!isShowing()) {
            return;
        }
        showing = false;
        window.dismiss();
    }

    /**
     * 查看用户信息
     */
    private void checkUserInfo() {
        if (!canAddFriend && !isFriend) {
            hide();
            return;
        }
        try {
            // 跳转信息
            Intent intent = new Intent(mContext, UserInfoActivity.class);
            intent.putExtra("userInfo", userInfo);
            // fromGroupShow 可复用标签，暂不添加新标志区分，自身不显示其他信息
            intent.putExtra("fromGroupShow", true);
            intent.putExtra("privateChat", groupInfo.privateChat);
            intent.putExtra("groupInfo", groupInfo);
            List<GroupMember> groupMembers = ChatManager.Instance().getGroupMembers(target, false);
            GroupMember groupMember = null;
            for (GroupMember member : groupMembers) {
                if (member.memberId.equals(ChatManager.Instance().getUserId())) {
                    groupMember = member;
                }
            }
            if (groupMember != null) {
                intent.putExtra("normal", (groupMember.type == GroupMember.GroupMemberType.Normal) ? "1" : "0");
            } else {
                intent.putExtra("normal", "1");
            }
            mContext.startActivity(intent);
            hide();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加好友请求
     */
    private void addFriendRequest() {
        try {
            Intent intent = new Intent(mContext, InviteFriendActivity.class);
            intent.putExtra("userInfo", userInfo);
            mContext.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            hide();
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_cancel_pop) {
            hide();
        } else if (id == R.id.iv_pop_head) {
            checkUserInfo();
        } else if (id == R.id.btn_add_friend) {
            addFriendRequest();
        }
    }

}
